<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\BugDB;
use Illuminate\Http\Request;

class BugController extends BaseController
{
    /**
     *
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new BugDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $statuss = array(1,2);
        $resDb = $this->db->getIndex($statuss);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 历史
     */
    public function getHistory(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $statuss = array(3);
        $resDb = $this->db->getIndex($statuss);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $name = $request->post('name','');
        $link = $request->post('link','');
        $detail = $request->post('detail','');
        $genre = $request->post('genre',1);
        $alm = $request->post('alm',0);
        $genres = $this->db->getGenres();
        if (!$name) {
            return $this->toAppJson(-3);
        }
        if (!array_key_exists($genre,$genres)) {
            return $this->toAppJson(-3);
        }
        $resDb = $this->db->setAdd($adminId,$alm,$genre,$name,$link,$detail);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5);
        }
        return $this->toAppJson(0);
    }

    /**
     * 详情
     */
    public function getOne(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $bugId = $request->post('bugId',0);
        $resDb = $this->db->getOne($bugId);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0,$dataArr);
    }
}
